import 'package:convergence_media/di/di.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/get_catgory_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/get_goods_list_request.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/get_goods_list_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/index_get_goodslist_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/index_get_topic_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/index_get_user_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/login_token_login_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_add_goods_comment_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_add_new_goods_request.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_add_new_goods_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_flow_group_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_follow_user_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_like_comment_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_like_goods_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_my_follow_list_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_my_goods_list_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_my_like_goods_list_response.dart';
import 'package:convergence_media/features/api/dtos/circle_csavs_dtos/user_up_img_response.dart';
import 'package:dio/dio.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'circle_csavs_api.g.dart';

@riverpod
CircleCsavsApi circleCsavsApi(CircleCsavsApiRef ref) =>
    CircleCsavsApi(dio: ref.watch(circleDioProvider));

/// {@template circle_csavs_api}
/// 创视校园圈子
/// {@endtemplate}
class CircleCsavsApi {
  /// {@macro circle_csavs_api}
  const CircleCsavsApi({required Dio dio}) : _dio = dio;

  /// nodoc
  final Dio _dio;

  /// /login/tokenLogin
  /// 综合平台登录
  FutureOr<LoginTokenLoginResponse> postLoginTokenLogin(
    String logintoken,
  ) async {
    final uri = Uri(
      path: '/login/tokenLogin',
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'logintoken': logintoken,
      }),
    );

    return LoginTokenLoginResponse.fromJson(response.data!);
  }

  /// /get_catgory
  /// 获取栏目分类
  FutureOr<GetCatgoryResponse> getGetCatgory() async {
    final uri = Uri(path: '/get_catgory');

    final response = await _dio.getUri<Map<String, dynamic>>(uri);

    return GetCatgoryResponse.fromJson(response.data!);
  }

  /// /get_goods_list
  /// 获取栏目内容
  FutureOr<GetGoodsListResponse> getGetGoodsList(
    GetGoodsListRequest getGoodsListRequest,
  ) async {
    final uri = Uri(
      path: '/get_goods_list',
      queryParameters: getGoodsListRequest.toJson(),
    );

    final response = await _dio.getUri<Map<String, dynamic>>(uri);

    return GetGoodsListResponse.fromJson(response.data!);
  }

  /// /user/up_img
  /// 上传视频/图片接口
  FutureOr<UserUpImgResponse> postUserUpImg(String filePath) async {
    final uri = Uri(path: '/user/up_img');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'file': MultipartFile.fromFile(filePath),
      }),
    );

    return UserUpImgResponse.fromJson(response.data!);
  }

  /// /user/addNewGoods
  /// 发布图文
  FutureOr<UserAddNewGoodsResponse> postUserAddNewGoods(
    UserAddNewGoodsRequest userAddNewGoodsRequest,
  ) async {
    final uri = Uri(
      path: '/user/addNewGoods',
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(userAddNewGoodsRequest.toJson()),
    );

    return UserAddNewGoodsResponse.fromJson(response.data!);
  }

  /// /index/getTopic
  /// 获取话题
  FutureOr<IndexGetTopicResponse> getIndexGetTopic() async {
    final uri = Uri(path: '/index/getTopic');

    final response = await _dio.getUri<Map<String, dynamic>>(uri);

    return IndexGetTopicResponse.fromJson(response.data!);
  }

  /// /user/myFollowList
  /// 我的关注的人列表
  FutureOr<UserMyFollowListResponse> postUserMyFollowList() async {
    final uri = Uri(path: '/user/myFollowList');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return UserMyFollowListResponse.fromJson(response.data!);
  }

  /// /user/flowGroup
  /// 我关注的圈子（分类）列表
  FutureOr<UserFlowGroupResponse> postUserFlowGroup() async {
    final uri = Uri(path: '/user/flowGroup');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return UserFlowGroupResponse.fromJson(response.data!);
  }

  /// /user/myGoodsList
  /// 我关注的帖子列表
  FutureOr<UserMyGoodsListResponse> postUserMyGoodsList(
    String pageSize,
    String page,
  ) async {
    final uri = Uri(path: '/user/myGoodsList');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'pageSize': pageSize,
        'pg': page,
      }),
    );

    return UserMyGoodsListResponse.fromJson(response.data!);
  }

  /// /user/myLikeGoodsList
  /// 我发布的帖子
  FutureOr<UserMyLikeGoodsListResponse> postUserMyLikeGoodsList() async {
    final uri = Uri(path: '/user/myLikeGoodsList');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return UserMyLikeGoodsListResponse.fromJson(response.data!);
  }

  /// /user/likeGoods
  /// 帖子点赞/取消点赞
  /// 访问既是翻转
  FutureOr<UserLikeGoodsResponse> postUserLikeGoods(String id) async {
    final uri = Uri(
      path: '/user/likeGoods',
      queryParameters: {
        'id': id,
      },
    );

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return UserLikeGoodsResponse.fromJson(response.data!);
  }

  /// /user/followUser
  FutureOr<UserFollowUserResponse> getUserFollowUser(
    String userId,
  ) async {
    final uri = Uri(
      path: '/user/followUser',
      queryParameters: {
        'user_id': userId,
      },
    );

    final response = await _dio.getUri<Map<String, dynamic>>(uri);

    return UserFollowUserResponse.fromJson(response.data!);
  }

  /// /user/addGoodsComment
  FutureOr<UserAddGoodsCommentResponse> postUserAddGoodsComment(
    List<String> content,
    String goodsId,
    String pid,
    String toName,
  ) async {
    final uri = Uri(path: '/user/addGoodsComment');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'content': content,
        'goods_id': goodsId,
        'pid': pid,
        'to_name': toName,
      }),
    );

    return UserAddGoodsCommentResponse.fromJson(response.data!);
  }

  /// /user/likeComment
  FutureOr<UserLikeCommentResponse> getUserLikeComment(String id) async {
    final uri = Uri(
      path: '/user/likeComment',
      queryParameters: {
        'id': id,
      },
    );

    final response = await _dio.getUri<Map<String, dynamic>>(uri);

    return UserLikeCommentResponse.fromJson(response.data!);
  }

  /// /index/GetUser
  FutureOr<IndexGetUserResponse> postIndexGetUser() async {
    final uri = Uri(path: '/index/GetUser');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return IndexGetUserResponse.fromJson(response.data!);
  }

  /// /index/getGoodslist
  FutureOr<IndexGetGoodslistResponse> postIndexGetGoodslist(
    String toId,
    String page,
  ) async {
    final uri = Uri(
      path: '/index/getGoodslist',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'to_id': toId,
        'page': page,
      }),
    );

    return IndexGetGoodslistResponse.fromJson(response.data!);
  }
}
